import pickle, UnitTest
from UnitTest import UnitTest
from Report import Report
from subprocess import call, Popen, PIPE

class TestImporter():
	def __init__(self):
		try:
			self.tests = pickle.load(open('tests.p', 'rb'))
		except IOError:
			self.tests = []
	
	def nextTestNumber(self):
		maxId = 0
		if len(self.tests) > 0:
			for x in self.tests:
				maxId = max(maxId, x.number)
		return maxId + 1
	
	def addTest(self, category, subcategory, description, result, initial):
		newTest = UnitTest(self.nextTestNumber(), sanitize(category), sanitize(subcategory), sanitize(description), sanitize(result), sanitize(initial))
		self.tests.append(newTest)		
		
	def removeAllTests(self):
		self.tests = []
		pickle.dump(self.tests, open('tests.p', 'wb'))
		
	def removeTests(self, start, end):
		for x in range(start, end + 1):
			self.removeTest(x)
				
	def removeTest(self, testNumber):
		for x in self.tests:
			if x.number == testNumber:
				self.tests.remove(x)
		pickle.dump(self.tests, open('tests.p', 'wb'))

	def generateScripts(self):
		nextTest = self.getUnscriptedTest()
		while nextTest != 0:
			self.generateTestScript(nextTest)
			nextTest = self.getUnscriptedTest()

	def getUnscriptedTest(self):
		for x in self.tests:
			if x.script == 0:
				return x
		return 0
	
	def generateTestScript(self, test):
		print('category: ' + test.category + ' - ' + test.subcategory)
		print('Action to perform: %s' % test.description)
		print('Expected results: %s' % test.behavior)
		cmd = ['java net.grinder.TCPProxy -console -http > test' + test.number + '.py']
		outputData = Popen(cmd, stdout=PIPE)
		print(outputData)
				
		
	def importTestFile(self, filename, category):
		subcategory = "unsorted"
		for x in open(filename).read().split('\n'):
			if x.count('?'):
				self.addTest(category.title(), subcategory.title(), self.getDesc(x), self.getRes(x), self.getInitial(x))
			else:
				subcategory = x
		pickle.dump(self.tests, open('tests.p', 'wb'))
		
		return self.tests
				
	def getDesc(self, strIn):
		for x in ['is', 'does', 'do', 'are']:
			if strIn.count(', ' + x):
				return strIn.split(', ' + x)[0]
		print('error parsing %s' % strIn)
		return strIn
		
	def getRes(self, strIn):
		for x in ['is', 'does', 'do', 'are']:
			if strIn.count(', ' + x):
				return x + strIn.split(', ' + x)[1].split('?')[0]
				
	def getInitial(self, strIn):
		for x in ['is', 'does', 'do', 'are']:
			if strIn.count(', ' + x):
				return strIn.split(', ' + x)[1].split('?')[1]

def sanitize(stringInput):
	return stringInput
	ret = ""
	for letter in stringInput:
		if not letter == '"':
			ret+=letter
		else:
			ret+='\"'
	return ret
			
		
#t = TestImporter()
#t.removeTests(1,6)
#t.removeAllTests()
#t.importTestFile('boards.txt', 'boards')
#t.importTestFile('members.txt', 'members')
#t.importTestFile('misc.txt', 'misc')


#r = Report(t.tests)
#for x in t.tests:
#	try:
#		print(str(x.number) + ' - ' + x.category + ':' + x.subcategory + ' - ' + x.description + '/' + x.behavior)
#	except:
#		print('ERROR')
#		print(x.number)
#		print(x.category)
#		print(x.subcategory)
#		print(x.description)
#		print(x.behavior)

